package com.traffic.fuwu.controller;


import com.traffic.fuwu.mybatis.pojo.Account;
import com.traffic.fuwu.mybatis.pojo.Module;
import com.traffic.fuwu.service.AccountService;
import com.traffic.fuwu.service.ModuleService;
import com.traffic.fuwu.service.RoleService;
import com.traffic.fuwu.util.MD5Util;
import com.traffic.fuwu.util.ModuleUtil;
import com.traffic.fuwu.vo.ModuleBo;
import com.traffic.fuwu.vo.VoStatus;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Churcy on 2017/1/5.
 */
@Controller
public class LoginController {
    private static final VoStatus voStatus = new VoStatus();

    @Autowired
    private AccountService accountService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private ModuleService moduleService;

    /**
     * 跳转到login.jsp
     *
     * @return
     */
    @RequestMapping(value = {"/login", ""}, method = RequestMethod.GET)
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView("login");
        return modelAndView;

    }

    /**
     * 处理登录请求
     *
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String[] head = null;
        Account account = new Account();
        String page = null;
        try {
            // 1.获取请求参数
            String accountName = request.getParameter("username");
            String accountPassword = request.getParameter("password");
            do {
                if (StringUtils.isAnyBlank(accountName, accountPassword)) {
                    break;
                }

                // 通过帐号和密码得到account
                account = accountService.login(accountName, MD5Util.GetMD5Code(accountPassword));

                if (null == account) {
                    head = new String[]{"登录失败，用户名或密码错误"};
                    map.put("head", head);
                    voStatus.setCode(0);
                    voStatus.setMessage("登陆失败，用户名或者密码错误");
                    map.put("status", voStatus);
                    page = "redirect:/login";
                    break;
                }
                page = "redirect:/index";
                // 验证account非空
                head = new String[]{"登录成功"};
                map.put("head", head);
                map.put("line", account);
                map.put("appKey", account.getAppKey());
                voStatus.setCode(1);
                voStatus.setMessage("登陆成功");
                map.put("status", voStatus);


                Map<String, Object> condition = new HashMap();
                condition.put("accountId", account.getAccountId());
                List<Module> result = moduleService.findByConditions(condition);
                // userService.findPermissionList(userId);
                // 3.2.1封装菜单了表为树形结构 (module)
                if (result == null) {
                    page = "redirect:/login";
                    break;
                }
                List<ModuleBo> moduleBoList = ModuleUtil.createTree(result);
                // 4.返回页面数据
                request.getSession().setAttribute("moduleList", moduleBoList);
                request.getSession().setAttribute("account", account);
                request.getSession().setAttribute("appKey", account.getAppKey());

            } while (false);
        } catch (Exception e) {
            page = "redirect:/login";
            e.printStackTrace();
        }

        return page;
    }
}
